package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.RoadLineFaultRepository;
import com.hualude.roadsign.repository.UserRepository;
import com.hualude.roadsign.service.RoadLineFaultHandleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by ding on 2017/8/14.
 */
@Service
public class RoadLineFaultHandleServiceImpl implements RoadLineFaultHandleService {
    @Resource
    private RoadLineFaultRepository roadLineFaultRepository;
    @Resource
    private UserRepository userRepository;

    public DTResponseDto getRoadLineFaultHandle4DT(DTRequestDto dtRequestDto, String actorIds, Integer orgId, Integer maintenanceStatus) throws Exception {
        List<DTLineFaultDto> dto4DTs = roadLineFaultRepository.getRoadLineFaultHandle4DT(dtRequestDto, actorIds, orgId, maintenanceStatus);
        Integer count = roadLineFaultRepository.getRoadLineFaultHandleCount4DT(dtRequestDto, actorIds, orgId, maintenanceStatus);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for (DTLineFaultDto dtMachineFaultDto : dto4DTs) {
            if (dtMachineFaultDto.getRoadLineMaintainerId() != null) {
                String maintainerName = userRepository.getUserNameByUserId(dtMachineFaultDto.getRoadLineMaintainerId());
                dtMachineFaultDto.setRoadLineMaintainerName(maintainerName);
            }
        }
        return dtResponseDto;
    }

    @Transactional
    public Integer addRoadLineMaintenanceContent(Integer lampFaultId, String lampMaintenanceContent) throws Exception {
        return roadLineFaultRepository.addRoadLineMaintenanceContent(lampFaultId, lampMaintenanceContent);
    }

    @Transactional
    public Integer finishRoadLineMaintenance(String monitorMaintenancePhoto, Integer monitorMaintainerId, String monitorMaintenanceTime, String monitorMaintenanceExplain, Integer monitorFaultId) throws Exception {
        return roadLineFaultRepository.finishRoadLineMaintenance(monitorMaintenancePhoto, monitorMaintainerId, monitorMaintenanceTime, monitorMaintenanceExplain, monitorFaultId);
    }

    public List<LineFaultDto> getRoadLineFaultHandleList4App(Integer orgId, Integer maintenanceStatus, String actorIds, Integer count, Integer size) throws Exception {
        return roadLineFaultRepository.getRoadLineFaultHandleList4App(orgId, maintenanceStatus, actorIds, count, size);
    }

    public LineFaultDto getRoadLineReceiptInfo4App(Integer faultId) throws Exception {
        LineFaultDto result = roadLineFaultRepository.getRoadLineReceiptInfo4App(faultId);
        LineFaultDto latlngDto = roadLineFaultRepository.getRoadLineLatLng(result.getRoadLineId());
        result.setLongitude(latlngDto.getLongitude());
        result.setLatitude(latlngDto.getLatitude());
        return result;
    }
}
